<?php
// No direct access
defined ('_JEXEC') or die();

jimport ('joomla.application.component.controllerform');

class ShipnhanhControllerOrder extends JControllerForm
{
	public function __construct($config = array())
	{
		$this->view_list = 'orders';
		parent::__construct($config);
	}
	
	// Notification
	public function postSaveHook($model, $validData)
	{
		$item = $model->getItem();
		$orderId = $item->get('id');
		$notificationModel = JModel::getInstance('Notification', 'ShipnhanhModel');
		$notificationModel->insert($orderId, sprintf(JText::_('COM_SHIPNHANH_SHIPNHANH_UPDATE_ORDER_TEXT'), JText::_('COM_SHIPNHANH_INFORMATION_LABEL')));
		
		$contactModel = JModel::getInstance('ContactInfo', 'ShipnhanhModel');
		$data = JRequest::getVar('jform', array(), 'post', 'array');
		
		if (isset($data['email']))
		{
			$id = $contactModel->findContactByEmail($data['email']);
			
			if ($id <= 0)
			{
				$id = $contactModel->insertContactInfo($data['customer'], $data['email'], $data['address'], $data['phone']);
			}
// 			else
// 			{
// 				$contactModel->updateContactInfo($id, $data['email'], $data['address'], $data['phone']);
// 			}
			
			$model->updateContactId($orderId, $id);
		}
	}
	
	public function billout()
	{
		$app = JFactory::getApplication();
		$cid = $app->getUserState("ids");
		$app->setUserState('ids', null);
		
		if ($cid == null)
			$cid = JRequest::getVar('cid', array(), 'post', 'array');
		
		$model = $this->getModel('orders');
		$items = $model->getData($cid);
		$numCustomer = $model->countCustomer($cid);
		$total = $model->getSumOf($cid, 'webcharge');
		$surcharge = $model->getSumOf($cid, 'surcharge');
		$feeModel = JModel::getInstance('Fees', 'ShipnhanhModel');
		$feeList = $feeModel->getList();
		$shipMode = JModel::getInstance('Shippingfees', 'ShipnhanhModel');
		$shipFee = $shipMode->getList();
		$currencyModel = JModel::getInstance('Currencies', 'ShipnhanhModel');
		$currencies = $currencyModel->getList();
		$prepaidsModel = JModel::getInstance('Prepaids', 'ShipnhanhModel');
		
		$prepaid;
		
		if ($numCustomer > 1)
		{
			// Set warning
			JError::raiseNotice( 100, JText::sprintf('COM_SHIPNHANH_N_ITEMS_BILLOUT', $numCustomer));
		}
		
		if (count($items) > 0)
		{
			$prepaid = $prepaidsModel->getPrepaidValue($items[0]->email);
		}
		
		JRequest::setVar('view', 'billout');
		JRequest::setVar('items', $items);
		JRequest::setVar('total', $total);
		JRequest::setVar('fees', $feeList);
		JRequest::setVar('shipfees', $shipFee);
		JRequest::setVar('surcharge', $surcharge);
		JRequest::setVar('currencies', $currencies);
		JRequest::setVar('prepaid', $prepaid);
		$this->display();
	}
	
	public function editmany()
	{
		$cid = JRequest::getVar('cid', array(), 'post', 'array');
		$model = $this->getModel('orders');
		$items = $model->getData($cid);
		
		JRequest::setVar('view', 'editmany');
		JRequest::setVar('items', $items);
		
		$this->display();
	}
	
	public function updatemany()
	{
		$cid = JRequest::getVar('cid', array(), 'post', 'array');
		$model = $this->getModel('orders');
		
		$selectedField = JRequest::getVar('select_field');
		
		if ($selectedField == 0)
		{
			$fieldname = "status";
			$value = JRequest::getVar('status_field');
			$table = JTable::getInstance('Status', 'Table');
			$table->load($value);
			$valueText = $table->description;
		}
		else if ($selectedField == 1)
		{
			$fieldname = "usorderid";
			$value = "'" . JRequest::getVar('text_input') . "'";
			$valueText = $value;
		}
		else
		{
			$fieldname = "shipus";
			$value = JRequest::getVar('text_input');
			$valueText = $value;
		}
		
		$model->updateDataField($cid, $fieldname, $value);
		
		// Add notification
		$notificationModel = JModel::getInstance('Notification', 'ShipnhanhModel');
		foreach ($cid as $id)
		{
			$notificationModel->insert($id, sprintf(JText::_('COM_SHIPNHANH_SHIPNHANH_UPDATE_ORDER_TEXT'), $fieldname . '['. $valueText .']'));
		}
		
		$items = $model->getData($cid);
		
		JRequest::setVar('view', 'editmany');
		JRequest::setVar('items', $items);
		
		$this->display();
	}
}